/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * ItemDetailPanel.java
 *
 * Created on Jan 19, 2010, 12:40:14 PM
 */

package org.acmwustl.bubble.gui;

import org.acmwustl.bubble.controllers.VirtualKeyboardInterface;
import org.acmwustl.bubble.logic.Item;

/**
 *
 * @author officer
 */
public class ItemDetailPanel extends javax.swing.JPanel
implements VirtualKeyboardInterface {

    private EditWaresFrame parent;
    private Item myItem;
    
    /** Creates new form ItemDetailPanel */
    public ItemDetailPanel(EditWaresFrame ewf, Item i) {
        initComponents();
        parent = ewf;
        myItem = i;

        this.setSize(520, this.getHeight());

        nameTextField.setText(i.getName());
        priceTextField.setText(i.getPriceAsString());
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        nameTextField = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        priceTextField = new javax.swing.JTextField();
        jButton1 = new javax.swing.JButton();

        jLabel1.setFont(new java.awt.Font("Dialog", 1, 18));
        jLabel1.setText("Name:");

        nameTextField.setEditable(false);
        nameTextField.setFont(new java.awt.Font("Dialog", 1, 18));
        nameTextField.setText("A Long Name Default");
        nameTextField.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                nameTextFieldMouseClicked(evt);
            }
        });

        jLabel2.setFont(new java.awt.Font("Dialog", 1, 18));
        jLabel2.setText("Price:");

        priceTextField.setEditable(false);
        priceTextField.setFont(new java.awt.Font("Dialog", 1, 18));
        priceTextField.setText("10.00");
        priceTextField.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                priceTextFieldMouseClicked(evt);
            }
        });

        jButton1.setFont(new java.awt.Font("Dialog", 1, 18));
        jButton1.setForeground(new java.awt.Color(255, 0, 51));
        jButton1.setText("X");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(nameTextField, javax.swing.GroupLayout.DEFAULT_SIZE, 232, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(priceTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 64, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(nameTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2)
                    .addComponent(priceTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButton1))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void nameTextFieldMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_nameTextFieldMouseClicked
        parent.setKeyboardFocus(this);
    }//GEN-LAST:event_nameTextFieldMouseClicked

    private void priceTextFieldMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_priceTextFieldMouseClicked
        parent.setNumpadFocus(this);
    }//GEN-LAST:event_priceTextFieldMouseClicked

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        parent.deleteWare(myItem);
    }//GEN-LAST:event_jButton1ActionPerformed

    public void receieveCharacter(char c) {
        if(Character.isLetter(c) || c == ' ') {
            myItem.setName(nameTextField.getText() + c);
            nameTextField.setText(myItem.getName());
        }
        if(Character.isDigit(c)) {
            String price = String.valueOf(myItem.getPriceInCents());
            price += c;
            myItem.setPriceInCents(Integer.valueOf(price));
            priceTextField.setText(myItem.getPriceAsString());
        }
        if(c == '<') {
            if(parent.isNumpadFocus(this)) {
                //300
                String price = String.valueOf(myItem.getPriceInCents());
                if(price.length() > 1) {
                    myItem.setPriceInCents(Integer.valueOf(price.substring(0,price.length()-1)));
                }
                if(price.length() <= 1) {
                    myItem.setPriceInCents(0);
                }
                priceTextField.setText(myItem.getPriceAsString());
            }
            if(parent.isKeyboardFocus(this)) {
                if(myItem.getName().length() > 0) {
                    myItem.setName(nameTextField.getText().substring(0,nameTextField.getText().length() - 1));
                    nameTextField.setText(myItem.getName());
                }
            }
        }
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JTextField nameTextField;
    private javax.swing.JTextField priceTextField;
    // End of variables declaration//GEN-END:variables

 }
